
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>14.导入图片纹理</title>
	<style>
		canvas {
            background-color: rebeccapurple;
		}
	</style>
</head>
<body>
	<canvas id="gl" style="background-color: rebeccapurple;"></canvas>
	<script type="x-shader/x-vertex" id="vertex">
		attribute vec4 a_position;


		attribute vec2 a_texCoord;
		uniform vec2 u_resolution;
		varying vec2 v_texCoord;
		void main() {
			gl_Position = a_position;
			v_texCoord = a_texCoord;
		}
	</script>
	
	<script type="x-shader/x-fragment" id="fragment">
		precision mediump float;
		uniform sampler2D u_image;
		varying vec2 v_texCoord;
		void main(){
			vec2 onePixel = vec2(1.0, 1.0) / 1300.;
			gl_FragColor = (
				texture2D(u_image, v_texCoord + vec2(-onePixel.x, 0.0))) / 1.0;;
		}
	</script>
	<script>
		 /** @type {HTMLCanvasElement} */   
		const canvas=document.getElementById("gl")
		const gl=canvas.getContext("webgl")
		canvas.width=1000
		canvas.height=1000
		//流程 1.创建着色器 编译 链接
		const vertex=document.getElementById("vertex").textContent
		const fragment=document.getElementById("fragment").textContent

		const vertexShader=gl.createShader(gl.VERTEX_SHADER)
		const fragmentShader=gl.createShader(gl.FRAGMENT_SHADER)
		gl.shaderSource(vertexShader,vertex)
		gl.shaderSource(fragmentShader,fragment)
		gl.compileShader(vertexShader)
		gl.compileShader(fragmentShader)
		const program=gl.createProgram()
		gl.attachShader(program,vertexShader)
		gl.attachShader(program,fragmentShader)
		gl.linkProgram(program)
		gl.useProgram(program)


		const a_position=gl.getAttribLocation(program,"a_position")
		const bufferData=gl.createBuffer()
		gl.bindBuffer(gl.ARRAY_BUFFER,bufferData)
		// var x1 = -0.5
		// var x2 = .5
		// var y1 = 0.5
		// var y2 = -.5
		// gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([
		// 	x1, y1,
		// 	x2, y1,
		// 	x1, y2,
		// 	x1, y2,
		// 	x2, y1,
		// 	x2, y2,
		// ]), gl.STATIC_DRAW);
		gl.bufferData(gl.ARRAY_BUFFER,new Float32Array([
			
		
			// -.5,.5,
			// .5,.5,
			// -.5,-.5,

			// -.5,-.5,
			// .5,.5,
			// .5,-.5,

			-.5,.5,
			.5,.5,
			-.5,-.5,

			-.5,-.5,
			.5,.5,
			.5,-.5,
			//左下上，右下左

			// -0.5,0,
			// 0.5,0,
			// 0,-0.5,
			
			// -0.5,0,
			// 0.5,0,
			// 0,0.5
		]),gl.STATIC_DRAW)
		gl.enableVertexAttribArray(a_position)
		
		gl.vertexAttribPointer(a_position,2,gl.FLOAT,false,0,0)

		gl.viewport(0,0,1000,1000)

		let image=new Image();
		image.src = "../images/1.jpg";
		image.onload=function(){
			const a_texCoord=gl.getAttribLocation(program,"a_texCoord")
			const bufferData=gl.createBuffer()
			gl.bindBuffer(gl.ARRAY_BUFFER,bufferData)
			gl.bufferData(gl.ARRAY_BUFFER,new Float32Array([
			0.0,  0.0,
			1.0,  0.0,
			0.0,  1.0,
			0.0,  1.0,
			1.0,  0.0,
			1.0,  1.0,
			]),gl.STATIC_DRAW)
			gl.enableVertexAttribArray(a_texCoord)
			
			gl.vertexAttribPointer(a_texCoord,2,gl.FLOAT,false,0,0)

			let texture = gl.createTexture();
			gl.bindTexture(gl.TEXTURE_2D, texture);
			
			// 设置参数，让我们可以绘制任何尺寸的图像
			gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
			gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
			gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
			gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
			// 将图像上传到纹理
			gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE,image);

			gl.drawArrays(gl.TRIANGLE_STRIP, 0, 6);
		}




		//画线
	</script>
</body>
</html>